/*
 * GastoForm.java
 *
 * Created on 12 de julio de 2008, 19:16
 */

package View;

import Model.Congreso;
import Model.Gasto;
import Servicio.Proxy;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.ArrayList;

/**
 *
 * @author  Gigin
 */
public class GastoForm extends javax.swing.JDialog {
    private Gasto mGasto;
    private Congreso mCongreso;
    private ECajaForm caja;
    private ArrayList<String> mTipoGastos;
    
    /** Creates new form GastoForm */
    public GastoForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        mTipoGastos = Proxy.obtenerConjuntoExpandible(9);
        initComponents();   
        jComboTipoGastos.removeAllItems();
        for (int i=0; i<mTipoGastos.size();i++)
            jComboTipoGastos.addItem(mTipoGastos.get(i));
        
        this.caja = (ECajaForm)parent;
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextMonto = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextDescripcion = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jComboTipoGastos = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 10));
        jLabel1.setText("Monto: $");

        jLabel2.setFont(new java.awt.Font("Verdana", 0, 10));
        jLabel2.setText("Descripción:");

        jTextMonto.setFont(new java.awt.Font("Verdana", 0, 10));

        jTextDescripcion.setColumns(20);
        jTextDescripcion.setFont(new java.awt.Font("Verdana", 0, 10));
        jTextDescripcion.setRows(5);
        jTextDescripcion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextDescripcionKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTextDescripcion);

        jButton1.setFont(new java.awt.Font("Verdana", 1, 10));
        jButton1.setForeground(new java.awt.Color(94, 148, 189));
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Aceptar_normal.gif"))); // NOI18N
        jButton1.setToolTipText("Agregar gasto");
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Aceptar_deshabilitado.gif"))); // NOI18N
        jButton1.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Aceptar_presionado.gif"))); // NOI18N
        jButton1.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Aceptar_mouse.gif"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Verdana", 1, 10));
        jButton2.setForeground(new java.awt.Color(94, 148, 189));
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Cancelar_normal.gif"))); // NOI18N
        jButton2.setToolTipText("No agregar gasto");
        jButton2.setBorderPainted(false);
        jButton2.setContentAreaFilled(false);
        jButton2.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Cancelar_deshabilitado.gif"))); // NOI18N
        jButton2.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Cancelar_presionado.gif"))); // NOI18N
        jButton2.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Cancelar_mouse.gif"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Monotype Corsiva", 1, 24));
        jLabel3.setForeground(new java.awt.Color(94, 148, 189));
        jLabel3.setText("Nuevo Gasto");

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/logo_GTV4.gif"))); // NOI18N

        jComboTipoGastos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(389, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jTextMonto, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jComboTipoGastos, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap(147, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(49, 49, 49)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(42, 42, 42)
                            .addComponent(jLabel3))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(11, 11, 11)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(284, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextMonto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jComboTipoGastos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(51, 51, 51)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 235, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap()))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Calendar calendar = Calendar.getInstance();
    
    try{
        Float.parseFloat(jTextMonto.getText());
        if (jTextDescripcion.getText().compareTo("") != 0){
            mGasto = new Gasto(jTextDescripcion.getText(), Float.parseFloat(jTextMonto.getText()) ,
                 format.format(calendar.getTime()),jComboTipoGastos.getSelectedItem().toString());
            Proxy.modificarGasto(mCongreso, mGasto);
            JOptionPane.showMessageDialog(new JFrame(), "El gasto se ha almacenado con éxito", "Atención", JOptionPane.INFORMATION_MESSAGE);
            caja.actualizar();
            this.setVisible(false);
        }else{
            JOptionPane.showMessageDialog(new JFrame(), "Debe ingresar una descripción", "Atención", JOptionPane.WARNING_MESSAGE);
        }    
    }catch (Exception e){
        JOptionPane.showMessageDialog(new JFrame(), "El monto debe ser un número", "Atención", JOptionPane.WARNING_MESSAGE);
    }
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    caja.actualizar();
    this.setVisible(false);
}//GEN-LAST:event_jButton2ActionPerformed

private void jTextDescripcionKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextDescripcionKeyReleased
if(jTextDescripcion.getText().length()>200){
            JOptionPane.showMessageDialog(new JFrame(), "La longitud máxima es de 200 caracteres"," ", JOptionPane.INFORMATION_MESSAGE);
            jTextDescripcion.setText(jTextDescripcion.getText().substring(0,200));
    }

}//GEN-LAST:event_jTextDescripcionKeyReleased

   public void setmTipoGastos(ArrayList<String> val){
       this.mTipoGastos = val;
   }
   
   public ArrayList<String> getTipoGastos(){
       return this.mTipoGastos;
   }
   
   public void setCongreso(Congreso val){
        this.mCongreso = val;
   }
   
   public Congreso getCongreso(){
        return this.mCongreso;
   }
    /**
    * @param args the command line arguments
    */
/*    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GastoForm dialog = new GastoForm(new javax.swing.JFrame(), true, new ECajaForm);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

  */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboTipoGastos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextDescripcion;
    private javax.swing.JTextField jTextMonto;
    // End of variables declaration//GEN-END:variables

}
